package org.otherworld.process;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;

import org.otherworld.util.MathUtils;

public class SystemInfoUtils {

	private String availableProcessors;
	private String freeMemory;
	private String maxMemory;
	private String totalMemory;
	private ArrayList<Hashtable<String, Object>> totalDiskSpaces;
	private String root;
	

	public SystemInfoUtils() {
		availableProcessors = Integer.toString(Runtime.getRuntime()
				.availableProcessors());
		freeMemory = Long.toString(Runtime.getRuntime().freeMemory());
		long mm = Runtime.getRuntime().maxMemory();
		if (mm == Long.MAX_VALUE) {
			maxMemory = "no limit";
		} else {
			maxMemory = Long.toString(MathUtils.byteToMegabyte(mm))+" MByte";
		}
		totalMemory = Long.toString(Runtime.getRuntime().totalMemory());
		File[] roots = File.listRoots();
		totalDiskSpaces = new ArrayList<Hashtable<String, Object>>();

		Hashtable<String, Object> freeSpaces;
		for (File root : roots) {
			freeSpaces = new Hashtable<String, Object>();
			freeSpaces.put("label", root.getAbsolutePath());
			freeSpaces.put("totalspace",
					MathUtils.byteToMegabyte(root.getTotalSpace())+" MByte");
			freeSpaces.put("freespace",
					MathUtils.byteToMegabyte(root.getFreeSpace())+" MByte");
			freeSpaces.put("usablespace",
					MathUtils.byteToMegabyte(root.getUsableSpace())+" MByte");
			totalDiskSpaces.add(freeSpaces);
		}

	}

	/**
	 * Visszaadja a processzorok számát
	 * 
	 * @return
	 */
	public String getAvailableProcessors() {
		return availableProcessors;
	}

	/**
	 * Visszaadja a szabad memóriát
	 * 
	 * @return
	 */
	public String getFreeMemory() {
		return freeMemory;
	}

	/**
	 * Visszaadja a maximum memóriát
	 * 
	 * @return
	 */
	public String getMaxMemory() {
		return maxMemory;
	}

	/**
	 * Visszaadja a teljes memóriát
	 * 
	 * @return
	 */
	public String getTotalMemory() {
		return totalMemory;
	}

	/**
	 * Visszaadja az egyes meghajtók terület információit
	 * 
	 * @return
	 */
	public ArrayList<Hashtable<String, Object>> getTotalDiskSpaces() {
		return totalDiskSpaces;
	}
	
	public void displayMemoryInformation() {
		SystemInfoUtils sys = new SystemInfoUtils();
		System.out.println("Free Memory: " + sys.getFreeMemory());
		System.out.println("Max Memory: " + sys.getMaxMemory());
		System.out.println("Total Memory: " + sys.getTotalMemory());
	}

	public void displayProcessorCount() {
		SystemInfoUtils sys = new SystemInfoUtils();
		System.out.println("Available processors: "
				+ sys.getAvailableProcessors());
	}
	
	public void displayHardDriveInformation() {
		SystemInfoUtils sys = new SystemInfoUtils();
		ArrayList<Hashtable<String, Object>> disks = sys.getTotalDiskSpaces();
		for (Hashtable<String, Object> disk : disks) {
			System.out.println("Label: " + disk.get("label"));
			System.out.println("Total: " + disk.get("totalspace"));
			System.out.println("Free: " + disk.get("freespace"));
			System.out.println("Usable: " + disk.get("usablespace"));
			System.out.println(" - ");
		}
	}
	
	public void displayFullSystemInformation() {
		SystemInfoUtils sys = new SystemInfoUtils();
		System.out.println("Available processors: "
				+ sys.getAvailableProcessors());
		System.out.println("Free Memory: " + sys.getFreeMemory());
		System.out.println("Max Memory: " + sys.getMaxMemory());
		System.out.println("Total Memory: " + sys.getTotalMemory());
		ArrayList<Hashtable<String, Object>> disks = sys.getTotalDiskSpaces();
		for (Hashtable<String, Object> disk : disks) {
			System.out.println("Label: " + disk.get("label"));
			System.out.println("Total: " + disk.get("totalspace"));
			System.out.println("Free: " + disk.get("freespace"));
			System.out.println("Usable: " + disk.get("usablespace"));
			System.out.println(" - ");
		}

		// public static void main(String[] args) {
		// SystemInfoUtils sys = new SystemInfoUtils();
		// System.out.println("Available processors: "+sys.getAvailableProcessors());
		// System.out.println("Free Memory: "+sys.getFreeMemory());
		// System.out.println("Max Memory: "+sys.getMaxMemory());
		// System.out.println("Total Memory: "+sys.getTotalMemory());
		// ArrayList<Hashtable<String,Object>> disks = sys.getTotalDiskSpaces();
		// for (Hashtable<String,Object> disk : disks) {
		// System.out.println("Label: " +disk.get("label"));
		// System.out.println("Total: "+disk.get("totalspace"));
		// System.out.println("Free: "+disk.get("freespace"));
		// System.out.println("Usable: "+disk.get("usablespace"));
		// System.out.println(" - ");
		// }
		// }

	}
}
